#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2019/2/28 9:48 PM
# @Author  : Mr.Dynasty
# @File    : bank.py
# @Software: PyCharm
# @license : Copyright(C), Mr.Dynasty


key_dic = {'login':'登录',
           'check':'查询余额',
           'input':'存钱',
           'output':'取钱'}
account_list = [
    {'name':'小李',
     'password':123456,
     'money':1000},
    {'name':'小王',
     'password':123456,
     'money':5000},
    {'name':'Mary',
     'password':123456,
     'money':10000},
    {'name':'David',
     'password':123456,
     'money': 20000},
]
ACCOUNT = ''
def show_screen():
    '''显示屏幕'''
    print('*' * 14, 'ATM自动存取款机', '*' * 14)
    print('{0:2}1、{1:15}2、{2:15}'.format(' ', key_dic['login'], key_dic['check']))
    print('{0:2}3、{1:15}4、{2:15}'.format(' ', key_dic['input'], key_dic['output']))
    print('*' * 14, 'ATM自动存取款机', '*' * 14)

def verify_password(account,password):
    '''验证密码'''
    for account_dic in account_list:
        if account == account_dic['name']:
           if password == account_dic['password']:
               return True
    return False

def verify_statue():
    '''验证登录状态'''
    return key_dic['login'] == '退出'

def login():
    '''登录操作'''
    account = input('请输入账号:\n')
    password = int(input('请输入密码:\n'))
    while not verify_password(account, password):
        account = input('无此账号或密码错误，请重新输入账号:\n')
        password = int(input('请输入密码:\n'))
    key_dic['login'] = '退出'
    print('登录成功！')
    return account

def logout():
    '''退出操作'''
    key_dic['login'] = '登录'
    print('退出成功！')

def check_money(account):
    '''查询余额'''
    for account_dic in account_list:
        if account == account_dic['name']:
            return account_dic['money']
    return None

def input_money(account, input_money):
    '''存钱'''
    for account_dic in account_list:
        if account == account_dic['name']:
            loacal_money = int(account_dic['money'])
            account_dic['money'] = loacal_money + input_money
            return True
    return False

def output_money(account, output_money):
    '''取钱'''
    for account_dic in account_list:
        if account == account_dic['name']:
            loacal_money = int(account_dic['money'])
            if loacal_money - output_money < 0:
                print('账户余额不足！')
                return False
            else:
                account_dic['money'] = loacal_money - output_money
                return True
    print('未知错误！')
    return False

def main():
    while True:
        show_screen()
        if verify_statue():
            key = input('请选择操作:\n')
        else:
            key = input('请先选择登录:\n')
        if key == '1':
            if verify_statue():
                logout()
            else:
                ACCOUNT = login()
                continue
        else:
            while not verify_statue():
                continue
            if key == '2':
                # 查询余额
                money = check_money(ACCOUNT)
                if money:
                    print('账户余额{}'.format(money))
                else:
                    print('未知错误!')
                continue
            elif key == '3':
                # 存钱
                try:
                    money = int(input('请输入要存入的数目：\n'))
                    if input_money(ACCOUNT, money):
                        print('存入成功!')
                    else:
                        print('存入失败!')
                except TypeError:
                    print('请输入正确的数目!')
                continue
            elif key == '4':
                # 取钱
                try:
                    money = int(input('请输入要取出的数目：\n'))
                    if output_money(ACCOUNT, money):
                        print('取出成功!')
                    else:
                        print('取出失败!')
                except TypeError:
                    print('请输入正确的数目!')
                continue
            else:
                print('操作错误！')
                continue

if __name__ == '__main__':
    main()





